//
//  ActivityGoalsOfLastSevenDaysView.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/1.
//

import Foundation
import MYModule
import UIKit

class ActivityGoalsOfLastSevenDaysView: View {
    
    var date: Date = Date()
    
    var list: [ActivityModel]? {
        
        didSet {
            
            guard let list = list else {
                return
            }
            
            for (i, datasource) in segmentDatasource.enumerated() {
                if i < list.count {
                    
                    let model = list[i]
                    
                    if datasource.title == "unit_week_abbr_\(date.weekday)".language {
                        datasource.isSelected = true
                    } else {
                        datasource.isSelected = false
                    }


                    datasource.firstValue = Double(model.step)
                    datasource.secondValue = Double(model.calorie)
                    datasource.thirdValue = Double(model.time/60)
                    if model.goalStep > 0 {
                        datasource.targetFirstValue = Double(model.goalStep)
                    }
                    if model.goalCalorie > 0 {
                        datasource.targetSecondValue = Double(model.goalCalorie)
                    }
                    
                    if model.goalTime > 0 {
                        datasource.targetThirdValue = Double(model.goalTime)
                    }
                    
                    if datasource.isSelected {
                        didSelectSegment(i)
                    }
                    
                }
                
            }
            
            segmentView.reloadData()
            
        }
        
    }
    

    override func makeUI() {
        super.makeUI()
        
        self.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(self.segmentView)
        self.segmentView.snp.makeConstraints { make in
            make.height.equalTo(100)
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(self.titleLabel.snp.bottom).offset(20)
        }
        
        self.addSubview(self.currentProgressView)
        self.currentProgressView.snp.makeConstraints { make in
            make.width.height.equalTo(100)
            make.top.equalTo(self.segmentView.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        let stackView = StackView()
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fillEqually
        stackView.backgroundColor = UIColor.background
        stackView.addArrangedSubview(self.distanceView)
        stackView.addArrangedSubview(self.kcalView)
        stackView.addArrangedSubview(self.durationView)
        
        self.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalTo(self.currentProgressView.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(48)
            make.bottom.equalTo(-20)
        }
        
        self.backgroundColor = UIColor.background
        self.titleLabel.text = "activity_last_seven_days_goal_title".language
        
        
    }
    
    private func didSelectSegment(_ index: Int) {
        self.segmentDatasource.enumerated().forEach { idx, model in
            
            model.isSelected = false
            
            if idx == index {
                model.isSelected = true
                
                self.currentProgressView.stepsView.progress = model.firstProgress
                self.currentProgressView.caloriesView.progress = model.secondProgress
                self.currentProgressView.durationView.progress = model.thirdProgress
                                
                self.distanceView.numLabel.text = Int(model.firstValue).normalTitle
                self.distanceView.unitLabel.text = "\(Int(model.targetFirstValue).normalTitle)"
                self.kcalView.numLabel.text = Int(model.secondValue).normalTitle
                self.kcalView.unitLabel.text = "\(Int(model.targetSecondValue).normalTitle) \("unit_calorie".language)"
                self.durationView.numLabel.text = Int(model.thirdValue).normalTitle
                self.durationView.unitLabel.text = "\(Int(model.targetThirdValue).normalTitle) \("unit_minute".language)"
            }
        }
        
        
        
        self.segmentView.reloadData()
    }
    
    //MARK: - lazy load -
    lazy var titleLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_000000
        label.font = Fonts.boldSystemFont(ofSize: 14)
        return label
    }()
    
    lazy var durationView: ActivityGoalsOfLastSevenDaysCurrentTotalItem = {
        let view = ActivityGoalsOfLastSevenDaysCurrentTotalItem()
        view.iconImageView.image = UIImage(named: "ic_home_today_steps_duration")
        view.titleLabel.text = "activity_duration".language
        view.numLabel.text = "--"
        view.unitLabel.text = "--\("unit_minute".language)"
        return view
    }()
    
    lazy var distanceView: ActivityGoalsOfLastSevenDaysCurrentTotalItem = {
        let view = ActivityGoalsOfLastSevenDaysCurrentTotalItem()
        view.iconImageView.image = UIImage(named: "ic_home_today_steps_steps")
        view.titleLabel.text = "unit_step".language
        view.numLabel.text = "--"
        view.unitLabel.text = "--"
        return view
    }()

    lazy var kcalView: ActivityGoalsOfLastSevenDaysCurrentTotalItem = {
        let view = ActivityGoalsOfLastSevenDaysCurrentTotalItem()
        view.iconImageView.image = UIImage(named: "ic_home_today_steps_calorie")
        view.titleLabel.text = "activity_calories".language
        view.numLabel.text = "--"
        view.unitLabel.text = "--\("unit_calorie".language)"
        return view
    }()
    
    private lazy var collectionViewflowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: kScreenWidth/7, height: 88)
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        return flowLayout
    }()
    
    lazy var segmentView: CollectionView = {
        let collectionView = CollectionView(frame: .zero, collectionViewLayout: collectionViewflowLayout)
        collectionView.backgroundColor = UIColor.background
        collectionView.register(GoalsOfLastSeventDaysSegmentCell.self, forCellWithReuseIdentifier: GoalsOfLastSeventDaysSegmentCell.reuseIdentifier)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    
    lazy var currentProgressView: ArcProgressBarView = {
        let progressView = ArcProgressBarView()
        return progressView
    }()
    
    lazy var segmentDatasource: [GoalsOfLastSeventDaysSegmentCellModel] = {
        var datasource = [GoalsOfLastSeventDaysSegmentCellModel]()
        
        let todayWeek = self.date.weekday
        if todayWeek < 7 {
            for i in (todayWeek+1)...7 {
                let model = GoalsOfLastSeventDaysSegmentCellModel()
                model.title = "unit_week_abbr_\(i)".language
                datasource.append(model)
            }
        }
        for i in 1...todayWeek {
            let model = GoalsOfLastSeventDaysSegmentCellModel()
            model.title = "unit_week_abbr_\(i)".language
            if i == todayWeek {
                model.isSelected = true
            }
            datasource.append(model)
        }
        
        return datasource
    }()

}

extension ActivityGoalsOfLastSevenDaysView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.segmentDatasource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GoalsOfLastSeventDaysSegmentCell.reuseIdentifier, for: indexPath) as! GoalsOfLastSeventDaysSegmentCell
        let model = self.segmentDatasource[indexPath.row]
        
        cell.model = model

        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    
        self.didSelectSegment(indexPath.row)
        
        
    }
}
